#import "SEGFocusView.h"

#import <Masonry/Masonry.h>

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif


#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif


#import <UIImageView+WebCache.h>
#import "UIImageView+SEGWebCache.h"

#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

/**************************SEGFocusView************************/
@implementation SEGFocusView
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kFocusViewHeight;
    }
    self = [super initWithFrame:frame];
    
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.contentTitleLabel];
    [self.bgView addSubview:self.onePictureImageVIew];

    [self addSubview:self.channelNameLabel];
    
    self.backgroundColor = kColor_W;
    
    return self;
}

-(void)setWithLayout:(SEGFocusViewLayout *)layout{
    self.height = layout.totalHeight;
    _layout = layout;
    
    if (self.height <= 0) return;
    
    self.contentTitleLabel.textLayout = layout.contentTitleLabelTextLayout;
    self.channelNameLabel.textLayout = layout.channelNameLabelTextLayout;
    
    [self.onePictureImageVIew setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,_layout.data.image]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];

    [self setupUI];
}

//界面布局
-(void)setupUI{
//    self.onePictureImageVIew.hidden = NO;
//    self.contentTitleLabel.hidden = NO;
//
//
    //背景
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.top.equalTo(self);
        make.height.mas_equalTo(bgViewHeight);
    }];
    
    //单张图
    [self.onePictureImageVIew mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self.bgView);
        make.height.width.mas_equalTo(bgViewHeight);
    }];
    
    //描述内容
    [self.contentTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView);
        make.left.equalTo(self.onePictureImageVIew.mas_right).mas_offset(10);
        make.height.mas_equalTo(_layout.contentTitleLabelHeight);
        make.right.mas_equalTo(self).mas_offset(-10);
    }];
    
    if (_layout.data.channelName && _layout.data.channelName.length > 0) {
        self.channelNameLabel.hidden = NO;
        self.channelNameLabel.backgroundColor = kColor_L3;
        self.channelNameLabel.layer.cornerRadius = 11.5;
        self.channelNameLabel.layer.masksToBounds = YES;
        //频道名称
        [self.channelNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView.mas_bottom).offset(10);
            make.left.equalTo(self.bgView);
            make.height.mas_equalTo(23);
            make.width.mas_equalTo(_layout.channelNameLabelWidth);
        }];
        
        self.channelNameLabel.textAlignment = NSTextAlignmentCenter;
        self.channelNameLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
    }else {
        self.channelNameLabel.hidden = YES;
    }
    
    self.contentTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.contentTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
}

#pragma mark - getter
- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = kColor_L3;
        
        _bgView.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(focusPGCContentViewTap)]) {
                [_self.delegate focusPGCContentViewTap];
            }
        }];
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

-(SEGLabel *)contentTitleLabel{
    if (!_contentTitleLabel) {
        _contentTitleLabel = [[SEGLabel alloc]init];
        _contentTitleLabel.userInteractionEnabled = YES;
        _contentTitleLabel.textAlignment = NSTextAlignmentCenter;
        _contentTitleLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _contentTitleLabel.displaysAsynchronously = YES;
        _contentTitleLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _contentTitleLabel.fadeOnHighlight = NO;
        _contentTitleLabel.numberOfLines = 2;
        _contentTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentTitleLabel.fadeOnAsynchronouslyDisplay = NO;
        _contentTitleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug)_contentTitleLabel.backgroundColor = kSegRandomColor;
    }
    return _contentTitleLabel;
}


-(UIImageView *)onePictureImageVIew{
    if (!_onePictureImageVIew) {
        _onePictureImageVIew = [[UIImageView alloc]init];
        _onePictureImageVIew.image = [UIImage imageWithContentsOfFile:SEGPicName(@"")];
        _onePictureImageVIew.contentMode = UIViewContentModeScaleAspectFill;
        _onePictureImageVIew.layer.masksToBounds = YES;
        //_onePictureImageVIew.layer.cornerRadius = 0.5;
        //if (kDebug)_onePictureImageVIew.backgroundColor = kSegRandomColor;
        _onePictureImageVIew.userInteractionEnabled = YES;
    }
    return _onePictureImageVIew;
}

-(SEGLabel *)channelNameLabel{
    if (!_channelNameLabel) {
        _channelNameLabel = [[SEGLabel alloc]init];
        _channelNameLabel.textAlignment = NSTextAlignmentCenter;
        _channelNameLabel.textVerticalAlignment = SEGTextVerticalAlignmentCenter;
        _channelNameLabel.displaysAsynchronously = YES;
        _channelNameLabel.ignoreCommonProperties = YES;//YES会影响系统属性
        _channelNameLabel.fadeOnHighlight = NO;
        _channelNameLabel.numberOfLines = 1;
        _channelNameLabel.fadeOnAsynchronouslyDisplay = NO;
        _channelNameLabel.fadeOnAsynchronouslyDisplay = NO;
        _channelNameLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        //if (kDebug)_channelNameLabel.backgroundColor = kSegRandomColor;
        
        _channelNameLabel.userInteractionEnabled = YES;
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
            if (_self.delegate && [_self.delegate respondsToSelector:@selector(focusChannelNameLabelTap:)]) {
                [_self.delegate focusChannelNameLabelTap:_channelNameLabel];
            }
        }];
        [_channelNameLabel addGestureRecognizer:tap];
    }
    return _channelNameLabel;
}


@end
